﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SystemDoc_docmanage_tree.aspx.cs" Inherits="Ccit.OA.SystemDoc.SystemDoc_docmanage_tree" %>
<%@ Register Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="Microsoft.Web.UI.WebControls" TagPrefix="iewc" %>
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>系统文档管理树</title>
    <script language="javascript" src="../script/jquery.js" type="text/javascript"></script>
    <script language="javascript" src="../script/StringClass_Ext.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jsonlib.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jbase64.js" type="text/javascript"></script>
    <style>
		.skin { BORDER-RIGHT: buttonhighlight 2px outset; BORDER-TOP: buttonhighlight 2px outset; FONT-SIZE: 9pt; VISIBILITY: hidden; BORDER-LEFT: buttonhighlight 2px outset; WIDTH: 100px; CURSOR: default; BORDER-BOTTOM: buttonhighlight 2px outset; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: absolute; BACKGROUND-COLOR: menu; TEXT-ALIGN: left }
		.menuitems { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-TOP: 5px; PADDING-BOTTOM: 5px;}
	</style>
</head>
<body onclick="hideMenu()">
    <ccit:ActionForm id="form1" runat="server">
    <!--树的切换-->
    <input type="hidden" id="_hidNodeTab" runat="server" />
    
    <input type="hidden" id="_hidClassType" runat="server" />
    <input type="hidden" id="_hidSysDocId" runat="server" />
    
    
    <input type="hidden" id="_hidOriUpAddrId" runat="server" />
    <input type="hidden" id="_hidUpAddrId" runat="server" />  
    <input type="hidden" id="_hidAddNewSign" runat="server" /> <!-- 新增记录标志 -->
    <input type="hidden" id="_hidDelDepAddressId" runat="server" />  
    <input type="hidden" id="_hidParaFolderShare" runat="server" /> <!-- 上级目录权限 -->
    
        <!--保存后传来的主键id-->
    <input type="hidden" id="_hidNewColumnId" runat="server" />
    <input type="hidden" id="_hidExpandSign" runat="server" />
    <input type="hidden" id="_hidLevelStruct" runat="server" />
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>    
          
    <iewc:TreeView ID="trvDepAddress" runat="server">
    </iewc:TreeView>
    <br />
    <!--编辑页面保存成功的时候  调用的事件-->
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display:none" value="触发事件" />
    <!--编辑页面删除成功的时候  调用的事件-->
    <input type="button" id="btnDelTrigger" onclick="doDelTrigger()" style="display:none" value="删除触发事件" />
    <script type="text/javascript">
    <!--
        var menuskin = "skin";
		var node = null;
		var menuname = "root";	 
		 
		function hideMenu() 
		{		    
			var obj = document.getElementById(menuname);
			if (null != obj && typeof(obj) != "undefined")
				document.getElementById(menuname).style.visibility = "hidden";
		}
		
		function highlighItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "highlight";
				event.srcElement.style.color = "white";
			}
		}
		function lowlightItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "";
				event.srcElement.style.color = "black";
				window.status = "";
			}
		}
		///右键菜单判断是否是子节点信息
		function clickItem() 
		{								
			if(node != null)
			{
				var vNodeId = node.getAttribute("ID");
//				alert(vNodeId);				
				var nodeType = node.getAttribute("type").toUpperCase();		
								
				var vNodeIdArray = vNodeId.split("_");
				var vAddressId = vNodeIdArray[0];
      		   // alert(vAddressId);		
			   clickItemInfo(vAddressId);
				 
			}
		}
		///右键菜单 新增和删除的链接url
//		function clickItemInfo(vSysDocID)
//		{		    
//		    if (event.srcElement.className == "menuitems" && event.srcElement.disabled != true) 
//	        {
//		        var sFuncName = event.srcElement.getAttribute("func");					
//		        switch (sFuncName)
//		        {
//			        case "Synchronous":	//同步创建
//			             var ClassType = $get("_hidClassType").value;
//                         var sParam1 = "SysDocID="+vSysDocID+"&classType="+ClassType+"&State=0";
//		                 var encodeParam = base64encode(sParam1); 
//				         parent.frames[1].location.href = "./SystemDoc_class_edit.aspx?"+encodeParam; 				
//				        break;
//			        case "Similar": //类型创建 
//			             var ClassType = $get("_hidClassType").value;
//                         var sParam1 = "SysDocID="+vSysDocID+"&classType="+ClassType+"&State=1";
//		                 var encodeParam = base64encode(sParam1); 
//				         parent.frames[1].location.href = "./SystemDoc_class_edit.aspx?"+encodeParam; 	
//				          break;
//				    case "CreateType"://子类别创建
//				         var ClassType = $get("_hidClassType").value;
//                         var sParam1 = "SysDocID="+vSysDocID+"&classType="+ClassType+"&State=0";
//		                 var encodeParam = base64encode(sParam1); 
//				         parent.frames[1].location.href = "./SystemDoc_class_edit.aspx?"+encodeParam; 	
//				             break;
//				    case "DeleteType":
//				          if(!confirm("是否删除该类别及下级类别信息?"))
//                            return;
//                        
//                        var nodeIndex = node.getNodeIndex();                            
//                        PageMethods.DeleteSysDcoMess(vSysDocID,nodeIndex,ClientCallBack);						    
//				        break;
//			        default:
//				        break;
//		        }	
//	         }
//		}
		///节点展开
		function trvDepAddress.onexpand()
		{	
		    /* Step 1：find current click TreeNode */	    
		    var nodeIndex = event.treeNodeIndex;	
		    //alert(nodeIndex);		
			if (typeof(nodeIndex) == "undefined"){			
				node = null;
				return false;
			}
			
			trvDepAddress.selectedNodeIndex = nodeIndex;
			node = trvDepAddress.getTreeNode(nodeIndex);
						
			/* Step 2: get asset type id，and get all child types */			
			var vNodeId = node.getAttribute("ID");
			var vNodeIdArray = vNodeId.split("_")
			var vAddressId = vNodeIdArray[0];
			var vLoadSign = vNodeIdArray[1];
			var vShare = vNodeIdArray[2];		
			$get("_hidParaFolderShare").value = vShare;   //获取上级目录权限
			if(vAddressId != "0"&&vLoadSign=="0") {
			   //if(vShare != "05")
			   //{
			    var oSubNodes = node.getChildren();
			    var oSubNode;
			    for(var i = oSubNodes.length - 1; i >= 0; i --) {
			        oSubNode = oSubNodes[i];
			        oSubNode.remove();
			    }
			     var expandSign = $get("_hidExpandSign").value;//是否逐级展开 1：是
			    var levelStruct = $get("_hidLevelStruct").value;			    
			    var ClassType = $get("_hidClassType").value;
			    var NodeTab = $get("_hidNodeTab").value;
			    PageMethods.GetSubAddrsByUpAddrId(vAddressId,nodeIndex,expandSign,levelStruct,ClassType,NodeTab,ClientBack);
			   //}
			   // else {
			        //node = null;
			   //      $get("_hidExpandSign").value = "0";  
			   //     node.setAttribute("EXPANDED",false); 
			   //     return false;
			   // }
			}
			hideMenu();
		}
		///节点收缩
		function trvDepAddress.oncollapse()
		{
			hideMenu();
		}
		///更改节点属性值的时候
		function trvDepAddress.oncontextmenu()
		{				
			hideMenu();
			var nodeIndex = event.treeNodeIndex;							
			if (typeof(nodeIndex) == "undefined")
			{
				node = null;
				return false;
			}
			trvDepAddress.selectedNodeIndex = nodeIndex;
			node = trvDepAddress.getTreeNode(nodeIndex);			
			var vNodeId = node.getAttribute("ID");			
			var vNodeIdArray = vNodeId.split("_");
			var vAddressId = vNodeIdArray[0];
			　
			
			if(vAddressId == "0") return;  //根节点不显示右键菜单
			　
			var obj = document.getElementById("root");				
			if (null == obj || typeof(obj) == "undefined")
			{
				node = null;
				return false;
			}
			
			var rightedge = document.body.clientWidth - event.clientX;		//右边距
			var bottomedge = document.body.clientHeight - event.clientY;	//底边距
			
			if (rightedge < obj.offsetWidth)
				obj.style.left = document.body.scrollLeft + 5 + event.clientX - obj.offsetWidth;
			else
				obj.style.left = document.body.scrollLeft + 5 + event.clientX ;
			
			if (bottomedge < obj.offsetHeight)
				obj.style.top = document.body.scrollTop + 5 + event.clientY - obj.offsetHeight ;
			else
				obj.style.top = document.body.scrollTop + 5 + event.clientY ;
											
			obj.style.visibility = "visible";
			return false;
		}	
		function ClientBack(vResult)
		{
		     var vNodeIdArray = vResult.split("_") 
		     var vOperType=vNodeIdArray[0];
		     switch(vOperType) {		       
                case "GetSubTypesByColumnId"://获取子节点
                     var vExeResult = vNodeIdArray[1];
	                 var nodeIndex = vNodeIdArray[2];
	                 var expandSign = vNodeIdArray[3];
	                 var levelStruct = vNodeIdArray[4];
	                 var parentNodeId = vNodeIdArray[5];
	                 
	                
    			 
                    eval("var objResult="+vExeResult); 
                    for(var i=0;i<objResult.length;i++)
                    {
                       var oResult=objResult[i];
                       createSubTNodes(nodeIndex,oResult);//展开老祖宗
                    }         
                
                if(expandSign == "1") {//是逐级展开
                    var parentIdArr = levelStruct.split(",");
                    var iLen = parentIdArr.length;
                    var bGet = false;
                    for(var i = 0; i < iLen; i ++) {                                    
                        var colId = parentIdArr[i];     
                        if(!bGet) {
                            if(colId == parentNodeId) {
                                if(i == (iLen - 1)) {//如果到最底层，则逐级展开标志0
                                    $get("_hidExpandSign").value = "0";                                
                                }
                                bGet = true;
                                continue;
                            }
                        }
                        else {//true，展开下级                             
                            var ctrlNode = findTreeNode(colId);
                            var nodeAttrId = ctrlNode.getAttribute("ID");    
                            var vNodeIdArray = nodeAttrId.split("_")
	                        if(vNodeIdArray[0] == "1") {
	                            if(i == (iLen - 1)) {
                                    ctrlNode.setAttribute("EXPANDED",true);                               
                                }
                                else
	                                continue;
	                        }
	                        else {
	                            ctrlNode.setAttribute("EXPANDED",true);
	                            break;
	                        }
                        }
                    } 
                 }
                   break;
                 case "GetNodeLevelStruct":
                     var vResult=vNodeIdArray[1];
                     eval("var objResult="+vResult); 
		             var levelStruct = objResult.resultIds;
		             $get("_hidLevelStruct").value = levelStruct;
                     
                     var parentIdArr = levelStruct.split(",");
                     var iLen = parentIdArr.length;
                     var colId = "";
                     var ctrlNode;
                     var nodeAttrId = "";
                     for(var i = 0; i < iLen; i ++) {
                        colId = parentIdArr[i];
                        ctrlNode = findTreeNode(colId);
                        nodeAttrId = ctrlNode.getAttribute("ID");
                                        
		                var vNodeIdArray = nodeAttrId.split("_")
			            if(vNodeIdArray[1] == "1") { //判断最一层节点是否展开过 1--：展开过
			                continue; 
			            }
			            else {
			                ctrlNode.setAttribute("EXPANDED",true);//没展开连续展开调trvDepAddress.onexpand()方法展开最老祖宗
			                break;
			            }
                     } 
                         objResult = null;     
                         break; 
                }
       		}
		function ClientCallBack(vResult) {
		    //alert(vResult);	    
		    var oXMLDoc = new ActiveXObject("Microsoft.XMLDOM");  
            oXMLDoc.loadXML(vResult);
            
            var vOperType = $(oXMLDoc).find("OPERTYPE").text();     //operation type
            var vExeResult = $(oXMLDoc).find("EXERESULT").text();   //execute result    
               
            var expandSign = $(oXMLDoc).find("ExpandSign").text();  //operation expandSign
            var levelStruct =$(oXMLDoc).find("LevelStruct").text(); //execute levelStruct   
            var parentNodeId=$(oXMLDoc).find("NodeId").text();      //execute NodeId   
            var nodeIndex;
            switch(vOperType) { 
                case "ADDRDELETE":  //delete address
                    nodeIndex = $(oXMLDoc).find("NODEINDEX").text();    
                    eval("var resultObj=" + vExeResult);
                    alert(resultObj.message);
                    if(parseInt(resultObj.resultSign) > 0) {
                        var node = $get("trvDepAddress").getTreeNode(nodeIndex); 
                        node.remove();
                        parent.frames[1].initializeAddNew();
                    }             
                    break;                 
            }
            
            oXMLDoc = null; 
        } 
        //追加子节点
        function createSubTNodes(nodeIndex,oResult) { 
                var sParamnodeShare = $get("_hidParaFolderShare").value;   //上级目录操作权限  (00完全,01只读,05无任何操作权限,06无任何操作权限但下级文件夹中有为 完全或只读权限 的)
                var sNewnodeShare = oResult.Sysnumber;  //当前目录权限
                if(sNewnodeShare != "01" && (sParamnodeShare == "01" || sParamnodeShare == "00"))
                {
                    sNewnodeShare = sParamnodeShare;   //当前目录不为只读时  自动继承上级目录权限;
                }
			     var node = $get("trvDepAddress").getTreeNode(nodeIndex);  
			     var ClassType = $get("_hidClassType").value;
			     var sParam1 = "folderID="+oResult.Sysdocid+"&classType="+ClassType+"&State=2"+"&Share="+sNewnodeShare;
		         var encodeParam1 = base64encode(sParam1);
                 vNavigateUrl = "./SystemDoc_docmanage_list.aspx?"+encodeParam1;
                 oSubNode = $get("trvDepAddress").createTreeNode();
                 
		         oSubNode.setAttribute("ID",oResult.Sysdocid + "_" +"0"+"_"+sNewnodeShare);
		         oSubNode.setAttribute("Text",oResult.Systypename);
		         oSubNode.setAttribute("Type","root");
		         
		         /* 只有权限为 只读或完全 时候 才有查看目录下文件的权限*/
		         if(sNewnodeShare == "00" || sNewnodeShare == "01")
		         {
		            oSubNode.setAttribute("Target","iFrmEdit");
		            oSubNode.setAttribute("NavigateUrl",vNavigateUrl);		            
		         }
		         oSubNode.setAttribute("ImageUrl","../images/toolbar/folder_open.gif");     
		     
		         if(oResult.State == "1") {  //if contain sub node then add empty node
		           oEmptyNode = $get("trvDepAddress").createTreeNode();  
		           oSubNode.add(oEmptyNode); 
		         }
		         node.add(oSubNode);
		        
		        /* set node load sign = 1 */
		        var vNodeId = node.getAttribute("ID");
		        var vNodeIdArray = vNodeId.split("_")
			    oResult.Sysdocid = vNodeIdArray[0]; 
			    node.setAttribute("ID",oResult.Sysdocid  + "_"+"1"+"_"+sParamnodeShare);		
			// }    
        }
        ///查找节点
        function findTreeNode(findAstTypeId) {
            var ctrlTree = document.getElementById("trvDepAddress");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); 
		    var ctrlChildNodes = ctrlRoot.getChildren();
	    
		    var ctrlFindNode = recursionFind(ctrlChildNodes,findAstTypeId);
            return ctrlFindNode;

        }
        
        function recursionFind(ctrlChildNodes,findAddrId) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vAddressId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vAddressId = vNodeIdArray[0];
		        if(vAddressId == findAddrId) {		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,findAddrId);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }
		                
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        }
        //编辑页面保存成功的时候  调用的方法
        function doTrigger() {		    
		     doOperOriParentNode();//找原父节点
             doOperDtParentNode();//找新父节点
		}
		//找原父节点
		function doOperOriParentNode() {
		    var vOriUpTypeId = $get("_hidOriUpAddrId").value; //原父节点的主键(绑定到树节点的主键)
		    var vUpTypeId = $get("_hidUpAddrId").value;		 //新父节点的主键(绑定到树节点的主键)
		    var isAddNew = $get("_hidAddNewSign").value;   //是否为编辑 1:编辑
		    
		        
		    if(isAddNew=="1"){//编辑
		        if(vUpTypeId == vOriUpTypeId){//原节点与新节点相等
		            var curNodeExpr = $get("_hidNewColumnId").value;   //获取的是当前编辑的节点对象 eg:{"columnId":"26","columnName":"测试类型","hasChild":"1"}
		            eval("var curNodeObj=" + curNodeExpr);		           
		            var colName = curNodeObj.Systypename;
		            var ctrClickObj = findTreeNode(curNodeObj.Sysdocid);		
		            ctrClickObj.setAttribute("Text",colName);
		            return;
		        }
		        /* 初始化全局变量 */
		        ctrlOgNode = null;		    
		        vOgNodeIndex = "";
		        // get root node object
		        var ctrlTree = document.getElementById("trvDepAddress");	
		        var ctrlRoot = ctrlTree.getTreeNode("0");
    		    
		        /* 获取原始上级节点对象 */
		        //根据原父节点主键获取其树的节点索引(eg：根据点击树主键3，获取其树的索引0.1)
		        if(vOriUpTypeId != ""&&vOriUpTypeId!="0") {
		            ctrlOgNode = findTreeNode(vOriUpTypeId);  //根据原父节点主键获取其树的节点对象 
		            vOgNodeIndex = ctrlOgNode.getNodeIndex();  //根据节点对象获取树节点索引eg:0.0.1
		        }
		        else {   //无上级父节点时
		            ctrlOgNode=ctrlRoot;  
		            vOgNodeIndex= "0";
		        }
		        var curNodeExpr = $get("_hidNewColumnId").value;   //获取的是当前编辑的节点对象 eg:{"columnId":"26","columnName":"测试类型","hasChild":"1"}
		        eval("var curNodeObj=" + curNodeExpr);
                // find current node position index
                var curNode = findTreeNode(curNodeObj.Sysdocid);  //获取当前编辑节点对象对应的树节点对象
                
                // delete origin node
                curNode.remove();  //移除
                
                curNodeObj = null;
		    }
		    return;		    
		}
		//找新父节点
		function doOperDtParentNode() {
		    var vOriUpTypeId = $get("_hidOriUpAddrId").value;  
		    var vUpTypeId = $get("_hidUpAddrId").value;		//新的上级节点    
		    var isAddNew = $get("_hidAddNewSign").value; //isAddNew = 1 时为编辑
		    
		    /* 初始化全局变量 */
		    ctrlDtNode = null;		    
		    vDtNodeIndex = "";
		    // get root node object
		    var ctrlTree = document.getElementById("trvDepAddress");	
		    var ctrlRoot = ctrlTree.getTreeNode("0");
		    /* 获取新上级节点对象 */
		    if(vUpTypeId != ""&& vUpTypeId!="0") {		        
		        ctrlDtNode = findTreeNode(vUpTypeId);	  //新上级节点对象	
		        if(null == ctrlDtNode) {    //找不到新父节点
		            expandDtNode(vUpTypeId);
		            return;
		        }
		        vDtNodeIndex = ctrlDtNode.getNodeIndex();	
		        var ctrlSubChildNodes = ctrlDtNode.getChildren();
		        var iChildLength = ctrlSubChildNodes.length;
		        
		        var bHasChild = false;
		        if(iChildLength > 0)
		           bHasChild = true;		    
		           
		        var ArrayID = ctrlDtNode.getAttribute("ID").split("_");
		        if(bHasChild == true){
		           if(ArrayID[1]=="0"){ //ArrayID[1]=="0"未展开 ;bHasChild != null 有子节点
		             ctrlDtNode.setAttribute("EXPANDED",true);
		             return ;
		          }
		       } 
		    }
		    else {	
		        ctrlDtNode=ctrlRoot;	
		        vDtNodeIndex= "0";  
		    } 
		    var curNodeExpr = $get("_hidNewColumnId").value;
		    eval("var curNodeObj=" + curNodeExpr);
		    
		    if(isAddNew=="1"){//编辑
		        if(vUpTypeId == vOriUpTypeId){//原节点与新节点相等
		            return;
		        }
		        else {		           
		            // add node to destination parent node	
		              createSubTNodes(vDtNodeIndex,curNodeObj);
		        }   
		    }
		    else //新增
		    {		       	                
		       createSubTNodes(vDtNodeIndex,curNodeObj);
		    } 
		    ctrlDtNode.setAttribute("EXPANDED",true);
		    curNodeObj = null;
		}
		
		function expandDtNode(dtNodeId) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    PageMethods.GetNodeLevelStruct(dtNodeId,ClientBack);  //获取新父节点上级节点
		}
		
		
		
		
		
		
		//编辑页面删除成功的时候  调用的方法
		function doDelTrigger() {		     
		    var delAddrId = $get("_hidDelDepAddressId").value;
		    ctrlNode = findTreeNode(delAddrId);
		    nodeIndex = ctrlNode.getNodeIndex();
		    var node = $get("trvDepAddress").getTreeNode(nodeIndex); 
            node.remove();            
		}
		
		function initialTreeNode(ctrlFindNode) {
		    var ctrlTree = document.getElementById("trvDepAddress");	
		    var vNodeId;
		    var vNodeIdArray;
		    var vAddrId;	
		    var vNodeIndex;
		    var vNodeState;
		    if(null != ctrlFindNode) {
		        vNodeId = ctrlFindNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_");
		        vAddrId = vNodeIdArray[0];
		        vNodeState = vNodeIdArray[2];
		        ctrlFindNode.setAttribute("ID",vAddrId + "_" +"0");
		        vNodeIndex = ctrlFindNode.getNodeIndex();
		        ctrlTree.selectedNodeIndex = vNodeIndex;
		        /* 如果当前节点没有下级节点，则不会触发expand事件,
		         * 此时需要在当前节点下增加一个空节点 */
		        var vChildNodes = ctrlFindNode.getChildren();
		        if(vChildNodes.length == 0) {
		           var oEmptyNode = $get("trvDepAddress").createTreeNode();  
		           ctrlFindNode.add(oEmptyNode);
		        }
		        ctrlFindNode.setAttribute("EXPANDED",true);
		    }
		}
    //-->
    </script>       
    </ccit:ActionForm>
</body>
</html>